<?php
// +----------------------------------------------------------------------
// | 手机招车
// +----------------------------------------------------------------------
namespace app\admin\controller;

use dc\controller\AdminBaseController;
use dragon\Support\Dictionary;
use dragon\Taxi\Driver;
use dragon\Taxi\TaxiCall;

class TaxiController extends AdminBaseController
{

    /**
     * 招车列表
     */
    public function index() {
        $keyword = $this->request->param('keyword', '', 'trim');
        $status = $this->request->param('status', 1, 'intval');

        $map = [];
        $map['status'] = $status;
        $keyword and $map['order_no|user_name|driver_name'] = ['like', '%'.$keyword.'%'];

        $list = TaxiCall::where($map)->order('update_time desc')->paginate($this->perpage);

        $this->assign('list', $list);
        $this->assign('status', $status);
        $this->assign('statuses', Dictionary::get('taxi_call_status'));
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    /**
     * 司机信息
     */
    public function driver() {
        $keyword = $this->request->param('keyword', '', 'trim');

        $map = [];
        $keyword and $map['name|code|mobile|car_no'] = ['like', '%'.$keyword.'%'];

        $list = Driver::where($map)->order('update_time desc')->paginate($this->perpage);

        $this->assign('list', $list);
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    /**
     * 司机信息
     */
    public function chooseDriver() {
        $keyword = $this->request->param('keyword', '', 'trim');

        $map = [];
        $keyword and $map['name|code|mobile|car_no'] = ['like', '%'.$keyword.'%'];

        $list = Driver::where($map)->order('update_time desc')->paginate($this->perpage);

        $this->assign('list', $list);
        $this->assign('order_no', $this->request->param('order_no'));
        $this->assign('page', $list->render());
        return $this->fetch('taxi/choose_driver');
    }

    public function chooseDriverAccept() {
        $taxiCall = TaxiCall::get(['order_no' => $this->request->param('order_no', '', 'trim')]);
        $taxiCall->save([
            'driver_name'   => $this->request->param('name', '', 'trim'),
            'driver_mobile' => $this->request->param('mobile', '', 'trim'),
            'car_no'        => $this->request->param('car_no', '', 'trim'),
            'status'        => 2,
        ]);
    }

    /**
     * 司机添加
     */
    public function driverAdd() {
        return $this->fetch('taxi/driver_form');
    }

    /**
     * 司机编辑
     */
    public function driverEdit() {
        $this->assign('info', Driver::get($this->request->param('id')));
        return $this->fetch('taxi/driver_form');
    }

    /**
     * 司机信息保存
     */
    public function driverSave() {
        $post = $this->request->post();
        if(empty($post['name'])) $this->error('请输入司机姓名');
        if(empty($post['mobile'])) $this->error('请输入司机手机号');
        if(empty($post['car_no'])) $this->error('请输入车牌号');

        if(empty($post['id'])) {
            $driver = new Driver();
        } else {
            $driver = Driver::get($post['id']);
        }
        $driver->save($post);

        $this->success('保存成功', url('taxi/driverEdit', ['id' => $driver['id']]));
    }


    // 完成运单
    public function finish() {
        $taxiCall = TaxiCall::get(['order_no' => $this->request->param('order_no')]);
        $taxiCall->save(['status' => 3,]);
        $this->success('操作成功');
    }

    // 招车详情
    public function cancel() {
        $taxiCall = TaxiCall::get(['order_no' => $this->request->param('order_no')]);
        $taxiCall->save(['status' => 0]);
        $this->success('操作成功');
    }
}